package com.easemob.helpdesk.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;


import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaolong.wei on 2019/4/2.
 */

public class AutoNewLineLayout extends LinearLayout {

    private static final String TAG = "AutoNewLineLayout";

    public AutoNewLineLayout(Context context) {
        this(context,null);
    }

    public AutoNewLineLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AutoNewLineLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        mContext = context;
//
//        TypedArray typedArray = context.obtainStyledAttributes(R.styleable.AutoNewLineLayout);
//        space = typedArray.getFloat(R.styleable.AutoNewLineLayout_space,20);
//
//        lineCount = new ArrayList<>();
//        typedArray.recycle();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (getChildCount() == 0){
            super.onLayout(changed,l,t,r,b);
            return;
        }
        int currentLineTotalWidth = 0;
        List<Integer> list = new ArrayList<>();
        list.add(0);
        View child = null;
        LayoutParams p = null;
        int currentLine = 0;
        for (int i = 0 ; i < getChildCount() ; i ++){
            child = getChildAt(i);
            p = ((LayoutParams) child.getLayoutParams());
            currentLineTotalWidth += child.getMeasuredWidth() + p.leftMargin + p.rightMargin;
            if (currentLineTotalWidth > getMeasuredWidth()){
                list.add(i);
                Log.d(TAG, "onLayout: i="+i);
                currentLineTotalWidth = child.getMeasuredWidth() + p.leftMargin + p.rightMargin;
            }


            int left = 0;
            int top = 0;
            if (list.contains(i)){//每一行的行首，即第一个TextView

                currentLine = list.indexOf(i);
                Log.d(TAG, "onLayout: i==="+i+"~~~top="+top);
            }else {
                currentLine = list.size() - 1;
                //循环每一行的textView计算当前view的left
                Log.d(TAG, "onLayout: currentLine = " + currentLine);
                for (int m = list.get(currentLine); m < i; m++) {
                    left += getChildAt(m).getMeasuredWidth() + ((LayoutParams) getChildAt(m).getLayoutParams()).leftMargin + ((LayoutParams) getChildAt(m).getLayoutParams()).rightMargin;
                }

            }
            left += p.leftMargin;
            top = (getChildAt(0).getMeasuredHeight() + p.topMargin + p.bottomMargin) * currentLine + p.topMargin;

            child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight());

        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        Log.d(TAG, "onMeasure: children count is "+getChildCount());
        if (getChildCount() == 0){
            super.onMeasure(widthMeasureSpec,heightMeasureSpec);
            return;
        }


        int lineCount = 1;
        if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED){
            int childrenTotalWidth = 0;
            View childView = null;
            LayoutParams params = null;
            for (int i = 0 ; i < getChildCount() ; i ++){
                childView = getChildAt(i);
                if (childView.getVisibility() != GONE) {
                    params = ((LayoutParams) childView.getLayoutParams());

                    measureChild(childView, widthMeasureSpec, heightMeasureSpec);

                    childrenTotalWidth += childView.getMeasuredWidth() + params.leftMargin + params.rightMargin;

                    if (childrenTotalWidth > width) {//折行
                        lineCount++;
                        childrenTotalWidth = childView.getMeasuredWidth() + params.leftMargin + params.rightMargin;
                    }
                }
            }
            LayoutParams layoutParams = (LayoutParams) getChildAt(0).getLayoutParams();

            height = (getChildAt(0).getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin) * lineCount;
        }

        setMeasuredDimension(width,height);

    }

}
